#ifndef MESSAGE_VIEW_H
#define MESSAGE_VIEW_H

#include <QWidget>
#include <QList>
#include <QString>
#include <QTimer>
#include <vector>
#include <QColor>
#include <QPropertyAnimation>

enum MessgeType
{
    seccuss, warning, error, info
};

static std::unordered_map <MessgeType, std::pair <QColor, const QString> > messageColorMap = {
    { seccuss, std::make_pair(QColor("#67C23A"), ":/ui/icons/check.svg")     },
    { warning, std::make_pair(QColor("#E6A23C"), ":/ui/icons/attention.svg") },
    { error,   std::make_pair(QColor("#F56C6C"), ":/ui/icons/close.svg")     },
    { info,    std::make_pair(QColor("#909399"), ":/ui/icons/info.svg")      },
};

class MessageItem : public QWidget {
    Q_OBJECT

public:
    MessageItem(const QColor& color, const QString& text = "", const QString& iconSrc = "", const unsigned int msec = 3000, QWidget *parent = nullptr);
    QTimer timer;
    void setPosition(const int y);
    void destroy();

    unsigned int time = 3000;

    QPropertyAnimation *animation;
signals:
    void timeOut(MessageItem *item);

private:
    QString text;
    unsigned int defalueWidth  = 300;
    unsigned int defalueHeight = 50;
};

class MessageView : public QWidget
{
    Q_OBJECT
public:
    explicit MessageView(QWidget *parent = nullptr);

    void send(const QString& str, MessgeType color, const unsigned int msec = 3000);

private:
    void destroy(MessageItem *item);
    void update();

    unsigned int messageItemHeight = 50;
    unsigned int spacing           = 10;
    QWidget *parent;
    std::vector <MessageItem *> viewList;
};

#endif // MESSAGE_VIEW_H
